package com.aiss.ws.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.aiss.ws.Entity.CurseItem;
import com.aiss.ws.R;
import com.aiss.ws.adapter.CourseListAdatper;
import com.aiss.ws.utils.Command;
import com.aiss.ws.utils.Futil;
import com.aiss.ws.utils.Url;
import com.lidroid.xutils.view.annotation.event.OnItemClick;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenqiang on 16/5/17.
 */
@EActivity(R.layout.activity_courselist)
public class CourseListActivity extends BaseActivity {

    private List<CurseItem> datas = new ArrayList<>();

    private CourseListAdatper adapter;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what==Command.HANDLECODE6){
                try {
                    JSONObject obj = (JSONObject) msg.obj;
                    JSONArray array = obj.getJSONArray("return_data");
                    for(int i = 0 ; i < array.length(); i++){
                        CurseItem item = new CurseItem();
                        item.setIdx(array.getJSONObject(i).getString("idx"));
                        item.setNamex(array.getJSONObject(i).getString("namex"));
                        datas.add(item);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                adapter = new CourseListAdatper(datas,context);
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if(Futil.getValue(context,Command.SESSION_KEY,2)!=null&&!Futil.getValue(context,Command.SESSION_KEY,2).toString().equals("")){
                            if(is_true==1||is_true==2){
//                                TrueTitleActivity_.intent(context).extra("idx",datas.get(position).getIdx()).extra("ftype",type).extra("is_true",is_true).start();
                            }else{
                                CourseDetailActivity_.intent(context).extra("idx",datas.get(position).getIdx()).extra("ftype",type).start();
                            }

                        }else{
                            RegistActivity_.intent(context).start();
                            showMessage("请登录");
                        }


                    }
                });

            }
        }
    };

    @ViewById(R.id.title)
    TextView title_tv;

    @ViewById(R.id.listView)
    ListView listView;

    private Context context = this;

    @Extra("")
    String type;

    @Extra
    int is_true;

    @Extra
    int my;


    @Click(R.id.back)
    void setBack(){
        finish();
    }

    @AfterViews
    void init(){
        title_tv.setText("课程列表");
        HashMap<String,String> map = new HashMap<>();
        if(my==1){
            map.put("type","my");
            title_tv.setText("我的课程");
        }else{
            map.put("type","list");
            map.put("parentx",type);
        }
        Futil.xutils(Url.COURSE,map,handler, Command.HANDLECODE6);
    }





}
